#include "../model/osdremoteservicemodel.h"
#include "../remoteservice/remoteerr.h"
#include "passwordinput.h"
#include "ui_passwordinput.h"
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include "keyboard.h"
#include <QPropertyAnimation>
#include <QKeyEvent>
#include "../control/mainwindowcontroller.h"
#include "viewglobal.h"
#include "../model/osdutilsmodel.h"
#include <QRegExp>
#include <QRegExpValidator>

PasswordInput::PasswordInput(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PasswordInput)
{

    m_num = "";
    m_name = "";

    ui->setupUi(this);

    this->setAttribute(Qt::WA_ShowModal);
    this->setWindowModality(Qt::ApplicationModal);
    this->setWindowFlags(Qt::FramelessWindowHint);

    ui->widget->installEventFilter(this);
    ui->widget->setAttribute(Qt::WA_TranslucentBackground, true);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    ui->pushButton->installEventFilter(this);
    ui->lineEdit->installEventFilter(this);
    ui->label->setText("");
    initFocusOrder();

    m_keyboard = new KeyBoard(this);
    m_keyboard->setGeometry(this->x(),
                            this->y()+this->height(),
                            m_keyboard->width(),m_keyboard->height());

    m_keyboardAnimation = new QPropertyAnimation(m_keyboard,"geometry",this);
    m_keyboardAnimation->setEasingCurve(QEasingCurve::InOutQuad);
    m_keyboardAnimation->setDuration(500);

    ui->pushButton->setFocus();
    ui->lineEdit->setContextMenuPolicy(Qt::NoContextMenu);

    connect(&OSDRemoteServiceModel::instance(),
            SIGNAL(notifyCall(BaseResp)),
            this,
            SLOT(callResponse(BaseResp)));
}

void PasswordInput::callResponse(BaseResp rv)
{
    if (!this->isVisible())
    {
        return;
    }
    if (rv.getCode() == RETURN_CODE_SUCCESS)
    {
        this->hide();
        MainWindowController::instance().changeSubPage(OSD::ViewGlobal::SUB_PAGE_MEET_LOADING);
        ui->lineEdit->setText("");
        ui->label->setText("");
        ui->pushButton->setFocus();
    }
    else if (rv.getCode() == RETURN_CODE_NEED_PWD)
    {
        ui->label->setText("请输入密码！");
    }
    else
    {
        ui->label->setText(rv.getCodeString());
    }
}

PasswordInput::~PasswordInput()
{
    delete ui;
}

void PasswordInput::initFocusOrder()
{
    QList<QObject *> orderlist;
    orderlist.clear();
    orderlist<< ui->lineEdit << NULL << NULL << NULL;
    m_ctrlOrder.insert(ui->pushButton, orderlist);

    orderlist.clear();
    orderlist<< NULL << ui->pushButton << NULL << NULL;
    m_ctrlOrder.insert(ui->lineEdit, orderlist);
}

bool PasswordInput::eventFilter(QObject *o, QEvent *e)
{
    if (ui->pushButton == o)
    {
        if (e->type() == QEvent::FocusIn)
        {
            QGraphicsDropShadowEffect *pEffect = new QGraphicsDropShadowEffect(ui->pushButton);
            pEffect->setOffset(0, 0);
            pEffect->setColor(QColor(41, 163, 255, 255));
            pEffect->setBlurRadius(30);
            ui->pushButton->setGraphicsEffect(pEffect);
            return true;
        }
        else if (e->type() == QEvent::FocusOut)
        {
            ui->pushButton->setGraphicsEffect(NULL);
            return true;

        }else if (e->type() == QEvent::MouseButtonRelease)
        {
            on_pushButton_released();
            return true;
        }
    }
    else if (ui->widget == o)
    {
        if (e->type() == QEvent::Paint)
        {
            QPainter painter(ui->widget);
            QPen pen;
            pen.setColor(QColor(1, 1, 9, 128));
            painter.setPen(pen);
            painter.setBrush(QBrush(QColor(1, 1, 9, 128)));
            painter.drawRect(ui->widget->rect());
            return true;
        }
    }

    if (e->type() == QEvent::KeyRelease)
    {
        QKeyEvent *ke = dynamic_cast<QKeyEvent*>(e);
        if (ke && ke->key() == Qt::Key_Escape)
        {
            this->hide();
            ui->lineEdit->setText("");
            ui->label->setText("");
            ui->pushButton->setFocus();
            return true;
        }

        if (ke && (ke->key() == Qt::Key_Up ||
                   ke->key() == Qt::Key_Down))
        {
            if (!m_ctrlOrder.contains(o))
            {
                return false;
            }

            QObject *target = m_ctrlOrder.value(o).at(OSDUtilsModel::instance().keyIndex(ke->key()));
            if (target)
            {
                QWidget* w = dynamic_cast<QWidget*>(target);
                if (w) w->setFocus();
            }
            return true;
        }
    }
    return false;
}

void PasswordInput::on_pushButton_released()
{
    ui->label->setText("");
    QString pwd = ui->lineEdit->text();
    if (pwd.isEmpty())
    {
        ui->label->setText("请输入密码！");
        return;
    }
    call(m_num, pwd, m_name);
}

void PasswordInput::call(QString num, QString pwd, QString name)
{
    OSDRemoteServiceModel::instance().call(num,pwd,name);
}

void PasswordInput::setConferenceParam(QString num, QString name)
{
    m_num = num;
    m_name = name;
}

void PasswordInput::showKeyBoard(QObject *o,int type)
{
    if(m_keyboard->y() >= this->height())
    {
        m_keyboard->setShowKeyBoard(o, type);
        m_keyboardAnimation->setStartValue(QRect(m_keyboard->x(), m_keyboard->y(), m_keyboard->width(), m_keyboard->height()));
        m_keyboardAnimation->setEndValue(QRect(m_keyboard->x(), m_keyboard->y()-m_keyboard->height(), m_keyboard->width(), m_keyboard->height()));
        m_keyboardAnimation->start();
    }
}

void PasswordInput::hideKeyBoard()
{
    if(m_keyboard->y() < this->height())
    {
        m_keyboardAnimation->setStartValue(QRect(m_keyboard->x(), m_keyboard->y(),m_keyboard->width(), m_keyboard->height()));
        m_keyboardAnimation->setEndValue(QRect(m_keyboard->x(), m_keyboard->y()+m_keyboard->height(), m_keyboard->width(), m_keyboard->height()));
        m_keyboardAnimation->start();
    }
}
